<?php

class Goods extends Admincontroll{

    private $catelogic;
    private $goodslogic;
    public function __construct() {
        $this->catelogic=  $this->load("cate");
        $this->goodslogic=  $this->load("goods");
        parent::__construct();
    }
    public function edit(){
        $id=isset($_REQUEST["id"])?intval($_REQUEST["id"]):0;
        if(!$id){
            return $this->showmessage("参数异常！","错误信息");
        }
        if(isset($_REQUEST["submit"])){
            $image_path="";
            if(isset($_FILES["image"])&&$_FILES["image"]["size"]>0){
             $res=$this->upload->upload_image($_FILES["image"]);
             if($res["status"]==0){
                 return $this->showmessage($res["msg"],"错误信息");
             }else{
                $image_path=$res["path"];
             }
            }
            
            $base_name=  isset($_REQUEST["base_name"])?$_REQUEST["base_name"]:"";
            $parent_id=isset($_REQUEST["parent_id"])?$_REQUEST["parent_id"]:0;
            $desc=isset($_REQUEST["desc"])?$_REQUEST["desc"]:"";
            $sort=isset($_REQUEST["sort"])?intval($_REQUEST["sort"]):0;
            $market_price=isset($_REQUEST["market_price"])?$_REQUEST["market_price"]:0;
            $sale_price=isset($_REQUEST["sale_price"])?$_REQUEST["sale_price"]:0;
            $status=isset($_REQUEST["status"])?1:0;
            $parentv_id=0;
            if(is_array($parent_id)){
                foreach ($parent_id as $key=>$v){
                    if($v==-1){
                        unset($parent_id[$key]);
                    }
                }
                $parent_path=  implode(",", $parent_id);
                $parentv_id=  end($parent_id);
            }
            if(!$base_name){
                 return $this->showmessage("请输入商品名称！","错误信息");
            }
            if($parentv_id==0){
                 return $this->showmessage("请选择商品分类！","错误信息");
            }
            if(!is_numeric($sale_price)){
                 return $this->showmessage("销售价请输入数字！","错误信息");
            }
            if(!is_numeric($market_price)){
                 return $this->showmessage("市场价请输入数字！","错误信息");
            }
             if(!is_numeric($sort)){
                 return $this->showmessage("排序请输入数字！","错误信息");
            }
             $w=PinYin::cn_explode(Common::strEscape($base_name));
             $zimu="";
             foreach ($w as $v){
                    $zimu.= PinYin::to_first($v) ;
             }
            $goodinfo=array(
            "base_name"=>  Common::strEscape($base_name),
            "cate_id"=>  intval($parentv_id),
            "sort"=>$sort,
            "sale_price"=>$sale_price,
            "market_price"=>$market_price,
            "`desc`"=>Common::strEscape($desc),
             "edit_time"=>  time(),
             "cate_path"=>$parent_path.",",
             "status"=>$status,
             "sx"=>$zimu,
             "pinyin"=>  PinYin::utf8_to(Common::strEscape($base_name))
            );
            if($image_path!=""){
                $goodinfo["image"]=$image_path;
            }
            $res=$this->goodslogic->update($goodinfo,"id={$id}");
            if($res){
                return $this->showmessage("编辑商品成功","成功信息","?goods/goodslist");
            }else{
                return $this->showmessage("编辑商品成功","失败信息");
            }
        }else{
            $goods=  $this->goodslogic->get_one("*","id={$id}");
            $goodscate=explode(",",$goods["cate_path"]);
            $pinfo=array();
            foreach($goodscate as $val){
                if($val){
                    $cate=  $this->catelogic->get_one("*","id={$val}");
                    $catelist=$this->catelogic->select("*","parent_id={$cate["parent_id"]}");
                    $data=array("data"=>$catelist,"selected"=>$cate["id"]);
                    $pinfo[]=$data;
                }
            }
            Temp::assign("goods", $goods);
            Temp::assign("pinfo", $pinfo);
        }
        Temp::assign("id", $id);
        Temp::display();
    }
    
    public function delete(){
        $id=isset($_REQUEST["id"])?intval($_REQUEST["id"]):0;
        if(!$id){
            return $this->showmessage("参数异常！","错误信息");
        }
       $res=$this->goodslogic->delete("id={$id}");
       if($res){
            return $this->showmessage("删除成功", "成功信息", "?goods/goodslist");
       }
        
    }


    public function goodslist(){
        $where="1=1";
        if(isset($_REQUEST["search"])){
           $base_name=  isset($_REQUEST["base_name"])?$_REQUEST["base_name"]:"";
           $parent_id=isset($_REQUEST["parent_id"])?$_REQUEST["parent_id"]:0;
           if($base_name){
               $where.=" and (base_name like '%{$base_name}%' or sx like'".strtolower($base_name)."%') ";
           }
           if(!empty($parent_id)&&isset($parent_id[0])){
               if($parent_id[0]!=0){
                   
          
                 
                   
                   
                   
               foreach ($parent_id as $k=>$val){
                   if($val==-1){
                        unset($parent_id[$k]);
                    }
               }
               $cate_str=  implode(",", $parent_id);
               $cate_str=$cate_str.",";
                $where.=" and cate_path like '{$cate_str}%' ";
                  $goodscate=$parent_id;
            $pinfo=array();
            foreach($goodscate as $val){
                if($val){
                    $cate=  $this->catelogic->get_one("*","id={$val}");
                    $catelist=$this->catelogic->select("*","parent_id={$cate["parent_id"]}");
                    $data=array("data"=>$catelist,"selected"=>$cate["id"]);
                    $pinfo[]=$data;
                }
            }
         Temp::assign("pinfo", $pinfo);
            
                
                
                
               }
           }
           
          
           
        }else{
            
            
             $pinfo=array();
             $db=$this->catelogic->select("*"," parent_id=0");
             
             $pinfo[]=array("data"=>$db,"selected"=>0);
             Temp::assign("pinfo", $pinfo);
        }
        
        
         
        
       
        $page=  isset($_REQUEST["page"])?intval($_REQUEST["page"]):1;
        $count=  $this->goodslogic->get_count($where);
        $list=$this->goodslogic->select_page("*", $where, $page,20,"order by id desc");
      
        if($list){
        foreach ($list as $k=>$val){
            $cateids=  explode(",", $val["cate_path"]);
            $catename_array=array();
            foreach ($cateids as $v){
                if($v){
                    $cate=  $this->catelogic->get_one("*","id={$v}");
                    $catename_array[]=$cate["base_name"];
                }
            }
            $list[$k]["thumb"]=  $this->getThumb($val["image"], "100x100");
            $list[$k]["cate_name"]=  implode(" > ", $catename_array);
        }
        $page_data = Common::getPages($count, $page, 20);
        Temp::assign("page_data", $page_data);
        Temp::assign("list", $list);
        }
        
        
        Temp::display();
    }
     //ajax批量上架商品
    public function public_ajax_up(){
        $ids=isset($_REQUEST["ids"])?$_REQUEST["ids"]:0;
        $msg=array();
        if(count($ids)>0){
            $ids_str=  implode(",", $ids);
            $sql="id in({$ids_str})";
            $goodsdata=array(
                "status"=>1,
                "edit_time"=>  time()
            );
            $res=$this->goodslogic->update($goodsdata,$sql);
       if($res){
           $msg["status"]=1;
       }else{
            $msg["status"]=-1;
       }
            
        }  else {
            $msg["status"]=0;
        }
        echo json_encode($msg);
    }

    
    //ajax批量下架商品
    public function public_ajax_down(){
        $ids=isset($_REQUEST["ids"])?$_REQUEST["ids"]:0;
        $msg=array();
        if(count($ids)>0){
            $ids_str=  implode(",", $ids);
            $sql="id in({$ids_str})";
            $goodsdata=array(
                "status"=>0,
                "edit_time"=>  time()
            );
            $res=$this->goodslogic->update($goodsdata,$sql);
       if($res){
           $msg["status"]=1;
       }else{
            $msg["status"]=-1;
       }
            
        }  else {
            $msg["status"]=0;
        }
        echo json_encode($msg);
    }
        //ajax批量删除商品
    public function public_ajax_delete(){
        $ids=isset($_REQUEST["ids"])?$_REQUEST["ids"]:0;
        $msg=array();
        if(count($ids)>0){
            $ids_str=  implode(",", $ids);
            $sql="id in({$ids_str})";
            $res=$this->goodslogic->delete($sql);
       if($res){
           $msg["status"]=1;
       }else{
            $msg["status"]=-1;
       }
            
        }  else {
            $msg["status"]=0;
        }
        echo json_encode($msg);
    }
    //后台默认首页
    public function index(){
        if(isset($_REQUEST["submit"])){
            $image_path="";
            if(isset($_FILES["image"])&&$_FILES["image"]["size"]>0){
             $res=$this->upload->upload_image($_FILES["image"]);
             if($res["status"]==0){
                 return $this->showmessage($res["msg"],"错误信息");
             }else{
                $image_path=$res["path"];
             }
            }
            
            $base_name=  isset($_REQUEST["base_name"])?$_REQUEST["base_name"]:"";
            $parent_id=isset($_REQUEST["parent_id"])?$_REQUEST["parent_id"]:0;
            $desc=isset($_REQUEST["desc"])?$_REQUEST["desc"]:"";
            $sort=isset($_REQUEST["sort"])?intval($_REQUEST["sort"]):0;
            $market_price=isset($_REQUEST["market_price"])?$_REQUEST["market_price"]:0;
            $sale_price=isset($_REQUEST["sale_price"])?$_REQUEST["sale_price"]:0;
            $status=isset($_REQUEST["status"])?1:0;
            $parentv_id=0;
            if(is_array($parent_id)){
                foreach ($parent_id as $key=>$v){
                    if($v==-1){
                        unset($parent_id[$key]);
                    }
                }
                $parent_path=  implode(",", $parent_id);
                $parentv_id=  end($parent_id);
            }
            if(!$base_name){
                 return $this->showmessage("请输入商品名称！","错误信息");
            }
            if($parentv_id==0){
                 return $this->showmessage("请选择商品分类！","错误信息");
            }
            if(!is_numeric($sale_price)){
                 return $this->showmessage("销售价请输入数字！","错误信息");
            }
            if(!is_numeric($market_price)){
                 return $this->showmessage("市场价请输入数字！","错误信息");
            }
             if(!is_numeric($sort)){
                 return $this->showmessage("排序请输入数字！","错误信息");
            }
             $w=PinYin::cn_explode(Common::strEscape($base_name));
             $zimu="";
             foreach ($w as $v){
                    $zimu.= PinYin::to_first($v) ;
             }
            $goodinfo=array(
            "base_name"=>  Common::strEscape($base_name),
            "cate_id"=>  intval($parentv_id),
            "sort"=>$sort,
            "image"=>$image_path,
            "sale_price"=>$sale_price,
            "market_price"=>$market_price,
            "`desc`"=>Common::strEscape($desc),
            "create_time"=>  time(),
            "edit_time"=>  time(),
            "cate_path"=>$parent_path.",",
            "status"=>$status,
            "sx"=>$zimu,
            "pinyin"=>  PinYin::utf8_to(Common::strEscape($base_name))
            );
           
           $resualt=  $this->goodslogic->insert($goodinfo);
           if($resualt){
               return  $this->showmessage("添加商品成功","成功信息","?goods/goodslist");
           }else{
               return  $this->showmessage("添加商品失败","错误信息");
           }
            
        }else{
           $db=$this->catelogic->select("id,parent_id as pid,base_name as name"," parent_id=0");
           Temp::assign("catelist", $db);
           
        }
        Temp::display();
        
    }
 
    
}
